<?php
include("general_include.php");
include "checklogin.php";
$page_name="dashboard.php";
include "top.php";
//include "left.php";
function findvalue($table,$fld_name,$fld_value,$find_value)
	{
		 if($fld_value=='')
				return false;
				
			$SelectTypeSql = "SELECT ".$find_value." FROM ".$table." WHERE $fld_name=".$fld_value;
			$SelectType = mysql_fetch_array(mysql_query($SelectTypeSql));
			return $SelectType[0];
	}	
$action=$_REQUEST['action'];
$user_id=$_SESSION['user_id'];
// Latest Message //
	$latest_msg = "SELECT *,DATE_FORMAT(`date_added` ,'%M  %d, %Y at %h %p' ) AS date_add 
					FROM ".TABLEPREFIX."_message WHERE user_id_to='".$user_id."' AND  user_id_from <>'".$user_id."'
					ORDER BY date_added DESC LIMIT 0,5";
	$latest_msgArr = $UserManagerObjAjax->GetRecords("All",$latest_msg);
	for($i=0;$i<count($latest_msgArr);$i++)
	{
		$userImg = "SELECT name,photo FROM ".TABLEPREFIX."_user WHERE user_id='".$latest_msgArr[$i]['user_id_from']."'";
		$ImgArr = $UserManagerObjAjax->GetRecords("Row",$userImg);
		
		$latest_msgArr[$i]['photo'] = $ImgArr['photo'];	
		$latest_msgArr[$i]['from_name'] = $ImgArr['name'];
		$flg_Ink='';
		if($latest_msgArr[$i]['reply_of_msg']!='0')
		{		
		    $latest_msg_lnk = "SELECT * FROM ".TABLEPREFIX."_message WHERE message_id=".$latest_msgArr[$i]['reply_of_msg'] ;
			$latest_msgArr_lnk = $UserManagerObjAjax->GetRecords("Row",$latest_msg_lnk);
			if($latest_msgArr_lnk['user_id_from']==$user_id)
			  $flg_lnk="out";
			else
			  $flg_lnk="in";
				
			 
				//echo $flg_lnk; 	
		}
	    else
		{
		    $flg_lnk="in"; 
		  
		}
		//echo $flg_Ink;
		if($flg_lnk=="in")
		{ 				
				if($latest_msgArr[$i]['reply_of_msg']!='0')
				  $latest_msgArr[$i]['link'] = 'inbox.php?pmsg_id='.$latest_msgArr[$i]['reply_of_msg'].'#'.$latest_msgArr[$i]['message_id'];
				else
	              $latest_msgArr[$i]['link'] = 'inbox.php#'.$latest_msgArr[$i]['message_id'];
				
		 }
		 if($flg_lnk=="out")
		 {
		 	$latest_msgArr[$i]['link'] = 'send_item.php#'.$latest_msgArr[$i]['message_id'];
		 }	
		 //echo $latest_msgArr[$i]['link'];
		
	}
// Latest Message //	

// Lesson //	
$CoachlessonSql = "SELECT *,DATE_FORMAT(`date_added` ,'%M  %d, %Y at %h %p' ) AS date_add  FROM ".TABLEPREFIX."_lesson WHERE coach_id='".$user_id."' ORDER BY date_added DESC LIMIT 0,5";
$CoachlessonArr = $UserManagerObjAjax->GetRecords("All",$CoachlessonSql);

$Numcoachlesson=count($CoachlessonArr);

for($g=0;$g<$Numcoachlesson;$g++)
{
	$CoachlessonArr[$g]['lesson_id'] = $CoachlessonArr[$g]['lesson_id'];
	$CoachlessonArr[$g]['lesson_title'] = $CoachlessonArr[$g]['lesson_title'];
	$CoachlessonArr[$g]['student_id'] = $CoachlessonArr[$g]['student_id'];
	
	$Sql_Name="SELECT name FROM ".TABLEPREFIX."_user WHERE user_id=".$CoachlessonArr[$g]['student_id'];
	$Sql_NameArr = $UserManagerObjAjax->GetRecords("Row",$Sql_Name);
	
	$CoachlessonArr[$g]['student_name'] = $Sql_NameArr['name'];
	
	$CoachlessonArr[$g]['lesson_price'] = number_format($CoachlessonArr[$g]['lesson_price'], 2, '.', '');
	
}
// Lesson //

// My Lesson //
$myLessonSql = "SELECT *,DATE_FORMAT(`date_added` ,'%M  %d, %Y at %h %p' ) AS date_add FROM ".TABLEPREFIX."_lesson WHERE student_id=".$user_id." ORDER BY date_added DESC LIMIT 0,5";
$myLesson = $UserManagerObjAjax->GetRecords("All",$myLessonSql);

$NumLesson=count($myLesson);

for($g=0;$g<$NumLesson;$g++)
{
	$myLesson[$g]['lesson_id'] = $myLesson[$g]['lesson_id'];
	$myLesson[$g]['lesson_title'] = $myLesson[$g]['lesson_title'];
	$myLesson[$g]['coach_id'] = $myLesson[$g]['coach_id'];
	
	$Sql_Name="SELECT name FROM ".TABLEPREFIX."_user WHERE user_id=".$myLesson[$g]['coach_id'];
	$Sql_NameArr = $UserManagerObjAjax->GetRecords("Row",$Sql_Name);
	
	$myLesson[$g]['coach_name'] = $Sql_NameArr['name'];
	$myLesson[$g]['lesson_price'] = number_format($CoachgameArr[$g]['lesson_price'], 2, '.', '');
	

}

// My Lesson //

//Video Review //
 $videoReview = "SELECT vr.*,DATE_FORMAT(vr.`date_added` ,'%M  %d, %Y at %h %p' ) AS vr_date,v.video_title,u.name,u.photo FROM ".TABLEPREFIX."_video_review vr,".TABLEPREFIX."_video v,".TABLEPREFIX."_user u WHERE vr.video_id=v.video_id AND  v.user_id='".$user_id."' AND vr.is_active='Y' AND u.user_id=vr.reviewed_by ORDER BY vr.date_added DESC LIMIT 0,5";
 $videoReviewArr = $UserManagerObjAjax->GetRecords("All",$videoReview);
 $Numvideo = count($videoReviewArr);
 for($vr=0;$vr<$Numvideo;$vr++)
 {
   //  $dateSql = "SELECT date_format('".$videoReviewArr[$vr]['date_added']."','%M  %d, %Y') as vr_date";
  //	$DateArr = $UserManagerObjAjax->GetRecords("Row",$dateSql);
	 $videoReviewArr[$vr]['review_date']=$DateArr['vr_date'];
	 
	$Usertype = "SELECT user_type_id FROM ".TABLEPREFIX."_user_type_user_relation WHERE user_id='".$videoReviewArr[$vr]['reviewed_by']."'";
	$typeArr = $UserManagerObjAjax->GetRecords("All",$Usertype);
	for($t=0;$t<count($typeArr);$t++)
	{
	  if($typeArr[$t][0] == 1)
	     $coach = 1;
	  if($typeArr[$t][0] == 3)
	     $partner = 1;	 
	}
	if($coach == 1)
	{ 
	   $videoReviewArr[$vr]['user_type']='Coach';
	   
	}
	else if($partner == 1)
	{
	   $videoReviewArr[$vr]['user_type']='Training Partner';
	  
	}  
	else 
	{ 
	   $videoReviewArr[$vr]['user_type']='Student';
	  
	}  
 }
// Video Review // 
 

// Video //
$CoachvdoSql = "SELECT *,DATE_FORMAT(`date_added` ,'%M  %d, %Y at %h %p' ) AS date_add FROM ".TABLEPREFIX."_video where user_id='".$user_id."' ORDER BY date_added DESC LIMIT 0,5";
$CoachvdoArr = $UserManagerObjAjax->GetRecords("All",$CoachvdoSql);
$Numcoachvdo=count($CoachvdoArr);

for($v=0;$v<$Numcoachvdo;$v++)
{
    $CoachvdoArr[$v]['game']= findvalue(TABLEPREFIX."_game","game_id",$CoachvdoArr[$v]['game_id'],"game_name");
	
}
// Video //


// Coach Game //
$CoachgameSql = "SELECT *,DATE_FORMAT(`date_added` ,'%M  %d, %Y at %h %p' ) AS date_add FROM ".TABLEPREFIX."_user_game where user_id='".$user_id."' and user_type_id='1' ORDER BY date_added DESC LIMIT 0,5";
$CoachgameArr = $UserManagerObjAjax->GetRecords("All",$CoachgameSql);
$Numcoachgame=count($CoachgameArr);

for($g=0;$g<$Numcoachgame;$g++)
{
   $CoachgameArr[$g]['game']= findvalue(TABLEPREFIX."_game","game_id",$CoachgameArr[$g]['game_id'],"game_name");
   if($CoachgameArr[$g]['experience']!='')
    $CoachgameArr[$g]['experience'] = substr($CoachgameArr[$g]['experience'],0,30).'...';
}
// Coach Game //

// Training Partner Game //
$tpgameSql = "SELECT *,DATE_FORMAT(`date_added` ,'%M  %d, %Y at %h %p' ) AS date_add FROM ".TABLEPREFIX."_user_game where user_id='".$user_id."' and user_type_id='3' ORDER BY date_added DESC LIMIT 0,5";
$tpgameArr = $UserManagerObjAjax->GetRecords("All",$tpgameSql);
$Numtpgame=count($tpgameArr);

for($g=0;$g<$Numtpgame;$g++)
{
   $tpgameArr[$g]['game']= findvalue(TABLEPREFIX."_game","game_id",$tpgameArr[$g]['game_id'],"game_name");
   if($tpgameArr[$g]['experience']!='')
    $tpgameArr[$g]['experience'] = substr($tpgameArr[$g]['experience'],0,30).'...';
}
// Training Partner Game //


//print_r($latest_msgArr);
$smarty->assign('is_coach',$is_coach);
$smarty->assign('is_partner',$is_partner);
$smarty->assign('latest_msgArr',$latest_msgArr);
$smarty->assign('latest_msgArrNum',count($latest_msgArr));
$smarty->assign('Numcoachlesson',$Numcoachlesson);
$smarty->assign('CoachlessonArr',$CoachlessonArr);
$smarty->assign('Numvideo',$Numvideo);
$smarty->assign('videoReviewArr',$videoReviewArr);
$smarty->assign('Numcoachvdo',$Numcoachvdo);
$smarty->assign('CoachvdoArr',$CoachvdoArr);
$smarty->assign('Numcoachgame',$Numcoachgame);
$smarty->assign('CoachgameArr',$CoachgameArr);
$smarty->assign('Numtpgame',$Numtpgame);
$smarty->assign('tpgameArr',$tpgameArr);
$smarty->assign('myLesson',$myLesson);
$smarty->assign('NumLesson',$NumLesson);
$smarty->display("dashboard.tpl");
include("footer.php");
?>